package Day021_反射_注解.ZuoYe;

import java.lang.reflect.Constructor;

/**
 * @author zhangyan
 * @date 2020/5/15 16:24
 */
public class Test2 {
        public static void main(String[] args) throws Exception {

            //获取Student类的Class对象
            Class c = Class.forName("Day021_反射_注解.ZuoYe.Student");

            //1:通过Class对象的方法创建
            Object obj1 = c.newInstance();
            System.out.println(obj1);

            //2:通过Constructor对象的方法创建
            Constructor cons = c.getConstructor(String.class,int.class);
            Object obj2 = cons.newInstance("张三",18);
            System.out.println(obj2);
        }
    }

class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
